<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Model\Info;
use App\Http\Controllers\Controller;

class InfoController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $info = Info::find(1);

        return view('admin.info.infos',['info' => $info]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.new');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // $file = $request->file('file');
        // $extension = $file->getClientOriginalExtension();
        
        // $info  = new Info();
        // $info->file_id=1;
        // $info->name =$request->input('name');
        // $info->description =$request->input('description');
        // $info->price =$request->input('price');
        // $info->imageurl =$request->input('imageurl');
        
        // $info->save();
        
        // return redirect('/admin/products');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit()
    {

        $data = Info::find(1);
        return view('admin.info.add', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {

        $this->validate($request, [
                'tel' => 'required',
                'email' => 'required',
                'wechat' => 'required',
                'address' => 'required',
                'hr' => 'required',
                // 'ios' => 'required',
                // 'android' => 'required',
                'mall' => 'required'
            ]);

        $info = Info::find(1);

        $info->tel = $request->input('tel');
        $info->email = $request->input('email');
        $info->wechat = $request->input('wechat');
        $info->address = $request->input('address');
        $info->hr = $request->input('hr');
        // $info->ios = $request->input('ios');
        // $info->android = $request->input('android');
        $info->mall = $request->input('mall');


        if ($info->save()) {
            
            $request->session()->pull('info');

            return redirect('admin/infos');
        }else{

            return view('admin.info.add', $info);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Info::destroy($id);
        return redirect('/admin/infos');
    }
}
